<?php


namespace app\common\lib;

use think\facade\Request as R;
use think\Exception;
class Request
{
    private static $message="参数错误";
    /**
     * 获取并检验GET参数
     * @access public
     * @param  string|array  $data 变量名
     */
    public static function get($data=""){
        try{
            if(!$data) return R::get();
            if(!is_array($data)){
                if(!$request_data = R::get($data)) throw new Exception(self::$message);
            }else{
                $request_data = R::get();
                foreach ($data as $key=>$item){
                    if(!isset($request_data[$key])) throw new Exception(self::$message);
                }
            }
            return $request_data;
        }catch (Exception $e){
            throw $e;
        }
    }
    /**
     * 获取并检验POST参数
     * @access public
     * @param  string|array  $data 变量名
     */
    public static function post($data=""){
        try{
            if(!$data) return R::post();
            if(!is_array($data)){
                if(!$request_data = R::post($data)) throw new Exception(self::$message);
            }else{
                $request_data = R::post();
                foreach ($data as $item){
                    if(!isset($request_data[$item])) throw new Exception(self::$message);
                }
            }

            return $request_data;
        }catch (Exception $e){
            throw $e;
        }
    }
}